﻿package model
{
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.*;
	
	import spark.transitions.CrossFadeViewTransition;
		
	public class ToolBox extends Sprite
	{
		//Scale Image
		private var loadedImageWidth:uint;
		private var loadedImageHeight:uint;			
		private var maxWidth:uint;
		private var maxHeight:uint;			
		private var ratioToUse:Number;
		
		//Text assignment
		private var format:TextFormat;
		
		//Create Rectangle
		private var rect:Shape;
		private var xFade:CrossFadeViewTransition;
	
		public function ToolBox():void
		{
			
		}
		
		// 6-4: convert true/false string values to boolean
		private function toBoolean(str:*):Boolean
		{
			if(typeof(str) == "boolean")
			{
				return Boolean(str)
				;
			}
			else if(str.toLowerCase() == "true")
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function xFadeTrans():CrossFadeViewTransition
		{
			xFade = new CrossFadeViewTransition();
			xFade.duration = 200;
			return xFade;
		}
		
		// 6-5: Return formatted number - convert from single digit to double digit
		public function formatNum(num:int):String
		{
			if (num <= 9) {
				return "0"+String(num);
			}
			return String(num);
		}
		
		public function formatDistance(str:*):String
		{
			var dis:int = str;
			var result:String;
			if(dis > 1000)
			{
				result = dis / 1000 + " km";
			}else
			{
				result = dis + " m";
			}
			return result;
		}
		
		// 6-6: Return Shape - Rectangle used for the background color
		public function createRectangle(w:Number, h:Number, lineThickness:Number, lineColour:uint, fillColour:uint):Shape
		{
			rect = new Shape();
			rect.graphics.lineStyle(lineThickness, lineColour);
			rect.graphics.beginFill(fillColour, 1);
			rect.graphics.drawRect(0,0,w,h);
			return rect;
		}
		
		// 6-7: Text Assignment - Returns a formatted object
		public function formatText(textFamily:String, size:int, bold:int, textColour:uint):TextFormat
		{
			format = new TextFormat();
			format.font = textFamily;
			format.size = size;
			format.bold = bold;
			format.color = textColour;
			return format;
		}
		
		// 6-8: Scale Image - Scales the image into the RHS
		public function scaleImage(loadedImage:Loader):Loader
		{
			loadedImageWidth = loadedImage.width;
			loadedImageHeight = loadedImage.height;			
			maxWidth = 650;
			maxHeight = 780;			
			ratioToUse=maxWidth/maxHeight;
			
			if ((loadedImageWidth/loadedImageHeight) >= ratioToUse)
			{
								
				if (loadedImageWidth <= maxWidth)
				{
					//image width and height remain the same
				}
				else
				{
					loadedImage.width=maxWidth;
					loadedImage.height=(maxWidth/loadedImageWidth)*loadedImageHeight;
				}
			}
			else
			{
				if (loadedImageHeight<=maxHeight)
				{
					//image width and height remain the same
				}
				else
				{
					loadedImage.height=maxHeight;
					loadedImage.width=(maxHeight/loadedImageHeight)*loadedImageWidth;
				}
			}
			
			//Default x and y positions
			loadedImage.x = 600;
			loadedImage.y = 90;
			
			//Center the image vertically
			if(loadedImage.height < maxHeight)
			{
				loadedImage.y = 90+((maxHeight-loadedImage.height)/2);				
			}
			
			if(loadedImage.width < maxWidth)
			{
				loadedImage.x = 600+((maxWidth-loadedImage.width)/2);				
			}
			return loadedImage;
		}

	}//end Toolbox Class
	
}///end package





























